/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.FiscalYear;
import org.crystalrange.enterprise.service.FiscalYearService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class FiscalYearListController extends BaseController {

    protected FiscalYearService fiscalYearService;
    protected FiscalYear selectedFiscalYear;
    protected Listbox listboxFiscalYear;
    protected Search searchFiscalYear;
    protected List<FiscalYear> fiscalYears;

    public FiscalYearService getFiscalYearService() {
        return this.fiscalYearService;
    }

    public void setFiscalYearService(FiscalYearService fiscalYearService) {
        this.fiscalYearService = fiscalYearService;
    }

    public FiscalYear getSelectedFiscalYear() {
        return this.selectedFiscalYear;
    }

    public void setSelectedFiscalYear(FiscalYear selectedFiscalYear) {
        this.selectedFiscalYear = selectedFiscalYear;
    }

    public List<FiscalYear> getFiscalYears() {
        return this.fiscalYears;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchFiscalYear = new Search(FiscalYear.class);
        this.searchFiscalYear.setMaxResults(100);
        this.searchFiscalYear.addFetch("createdBy");
        this.searchFiscalYear.addFetch("updatedBy");
        this.fiscalYears = this.fiscalYearService.search(this.searchFiscalYear);

        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemFiscalYearCreate(Event event) throws Exception {
        this.fiscalYearCreate();
    }

    public void onClick$menuitemFiscalYearEdit(Event event) throws Exception {
        this.fiscalYearEdit();
    }

    public void onClick$menuitemFiscalYearDelete(Event event) throws Exception {
        this.fiscalYearDelete();
    }

    public void onClick$menuitemFiscalYearSearch(Event event) throws Exception {
        this.fiscalYearSearch();
    }

    public void onClick$menuitemFiscalYearRefresh(Event event) throws Exception {
        this.fiscalYearRefresh();
    }

    public void onClick$toolbarbuttonFiscalYearCreate(Event event) throws Exception {
        this.fiscalYearCreate();
    }

    public void onClick$toolbarbuttonFiscalYearEdit(Event event) throws Exception {
        this.fiscalYearEdit();
    }

    public void onClick$toolbarbuttonFiscalYearDelete(Event event) throws Exception {
        this.fiscalYearDelete();
    }

    public void onClick$toolbarbuttonFiscalYearSearch(Event event) throws Exception {
        this.fiscalYearSearch();
    }

    public void onClick$toolbarbuttonFiscalYearRefresh(Event event) throws Exception {
        this.fiscalYearRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.fiscalYearEdit();
    }
    
    private void fiscalYearCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/fiscalYear.zul");
    }

    private void fiscalYearEdit() throws Exception {
        if (this.selectedFiscalYear != null) {
            this.execution.sendRedirect("~./zul/html/fiscalYear.zul?Id=" + this.selectedFiscalYear.getId().toString());
        }
    }

    private void fiscalYearDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxFiscalYear.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    FiscalYear fiscalYear = (FiscalYear) selectedItem.getValue();
                    this.fiscalYearService.remove(fiscalYear);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.fiscalYears = this.fiscalYearService.search(this.searchFiscalYear);
        this.binder.loadAll();
    }

    private void fiscalYearSearch() throws Exception {
        this.execution.setAttribute("searchFiscalYear", this.searchFiscalYear);

        Window window = (Window) this.execution.createComponents("~./zul/html/fiscalYearSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchFiscalYear = (Search) this.execution.getAttribute("searchFiscalYear");
        }

        this.fiscalYears = this.fiscalYearService.search(this.searchFiscalYear);
        this.binder.loadAll();
    }

    private void fiscalYearRefresh() {
        this.fiscalYears = this.fiscalYearService.search(this.searchFiscalYear);
        this.binder.loadAll();
    }
}
